package com.szholly.utils.freemarker;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.szholly.utils.freemarker.directive.IDirective;
import com.szholly.utils.spring.SpringBeanFactory;

/**
 * 自定义FreeMarker配置，重点解决自定义标签问题
 */
public class FreeMarkerConfigurer extends org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer {
	
	protected Map<String, Object> getCustomDirective(){
		return null;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void setFreemarkerVariables(Map<String, Object> value){
		if(value==null){
			value = new HashMap<String,Object>();
		}
		Map tableMap = SpringBeanFactory.getBeansByType(IDirective.class);
		Set<String> keySet = tableMap.keySet();
		for (String item : keySet) {
			Object directive = tableMap.get(item);
			String[] msgs = item.split("_");
			if(msgs.length==2){
				String key = msgs[1];
				if(value.containsKey(key)){
					value.remove(key);
				}
				value.put(key, directive);
			}
		}
		super.setFreemarkerVariables(value);
	}
}